<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2009                                               |
| http://e107shop.com                                                    |
| pbielen@gmail.com                                                      |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
+ ---------------------------------------------------------------------- +
*/

function e107shop_get_products_options($product_ID) {
    //
    // Grab the option groups
    //
    $sql = new db;
    // make data safe
    $product_ID = (int)$product_ID;
    if (!$sql->db_Select("e107shop_product_options", "option_group", "itemid='".$product_ID.
        "' group by option_group order by ID", "default", FALSE)) {
        return;
    } else {
        while ($row2 = $sql->db_Fetch()) {
            $option_group[] = $row2["option_group"];
        }
    }
    $counter = 0;
    $options = "<center><table>";
    foreach ($option_group as $group) {
        if ($counter > 0) $options .= "<tr><td colspan='3' style='height: 5px;'> </td></tr>";
        $options .= "<tr><td>".$group."</td><td width='10'>&nbsp;</td><td><select name='product_option".$counter."' class='tbox'>";
        $sql2 = new db;
        $sql2->db_Select("e107shop_product_options", "*", "itemid='".$product_ID."' and option_group='".
            $group."' ORDER BY ID ASC");
        while ($row = $sql2->db_Fetch()) {
            // check if the value is less or over 0, in that case extra stuff should be displayed.
            if ($row['optval'] == '' || $row['optval'] == 0) {
                $options .= "<option value='".$row["ID"]."'>".$row["optname"]."</option>";
            } else {
                $options .= "<option value='".$row["ID"]."'>".$row["optname"]." ".$row["opter"].
                " ".number_format($row["optval"], 2, ".", ",")."</option>";
            }
        }
        $options .= "</select></td></tr>";
        $counter++;

    }
    $options .= "</table></center><input type='hidden' name='option_count' value='".$counter."' />";
    return $options;
}

function e107shop_get_product_options_for_cart($option_ID, $field) {
    foreach (explode("|", $option_ID) as $options) {
        $options = (int)$options;
        $sql = new db;
        $sql->db_Select("e107shop_product_options", "*", "ID='".$options."'");
        while ($row = $sql->db_Fetch()) {
            if ($field == "optname") {
                $field2 .= $row["option_group"].": ".$row[$field]."<br>";
            } else {
                if ($row["opter"] == "+") {
                    $field2 += $row[$field];
                } else
                    if ($row["opter"] == "-") {
                        $field2 -= $row[$field];
                    }
            }
        }
    }
    return $field2;
}

function e107shop_get_product_field($product_ID, $field) {
    $sql = new db;
    $product_ID = (int)$product_ID;
    $sql->db_Select("e107shop_products", "*", "ID=".$product_ID."");
    while ($prodfield = $sql->db_Fetch()) {
        $field2 = $prodfield[$field];
    }
    return $field2;
}

function e107shop_get_option_field($option, $field) {
    $option = (int)$option;
    $sql = new db;
    $sql->db_Select("e107shop_product_options", "*", "ID=".$option);
    while ($prodoption = $sql->db_Fetch()) {
        $field2 = $prodoption[$field];
        $field3 = $prodoption['opter'];
        if ($field3  == "-") $field2 = $field3 . $field2;
    }
    return $field2;
}

function e107shop_get_user_for_order($order, $theuser) {
    global $sql, $tp;
    $order   = $tp->toDB($order);
    $theuser = $tp->toDB($theuser);
    if ($sql->db_Select("e107shop_orders", "*", "order_number='".$order."' AND theuser='".$theuser."'")) {
        $theresult = 1;
    }

    return $theresult;
}

function e107shop_get_file_listuser($download_file_name)
{
    global $shop_settings;
    $details .= "<option value=''>Select a Song...</option>";
    //
    // Get the download directory
    //
    foreach (glob($shop_settings["download_dir"]."/*") as $downloads) {

        $download_file_pre = str_replace($shop_settings["download_dir"]."/", "", $downloads);
        $download_file = str_replace(".mp3", "", $download_file_pre);

        if ($download_file_name == $download_file) {
            $details .= "<option selected value='".$download_file."'>".$download_file.
                "</option>";
        } else {
            $details .= "<option value='".$download_file."'>".$download_file."</option>";
        }
    }

    $details .= "</select>";

    return $details;
}

function e107shop_feature_field($field, $fieldname) {
    $details .= "<tr><td>".$fieldname."</td><td width='10'>&nbsp;</td><td><input class='tbox' type='text' name='".$field."' /></td></tr>";
    return $details;
}
